package com.week3.day02.atrShow;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ArtShowUtils {

    public static void main(String[] args) throws FileNotFoundException {
        List<ArtShow> list = ArtShowUtils.createList();
//        System.out.println(list);
        for (ArtShow artShow : list) {
            System.out.println(artShow);
        }
        System.out.println("===================");
        ArtShow artShow = ArtShowUtils.getMaxCount(list);
        System.out.println(artShow);

//        List<String> showedNames = ArtShowUtils.getShowedNames(list);
//        System.out.println(showedNames);
//

//        System.out.println("====================");
//        List<String> stuNames = ArtShowUtils.getStuNames(list);
//        System.out.println(stuNames);
//        ArtShowUtils.sortArtListByCount(list);
//        for (ArtShow artShow : list) {
//            System.out.println(artShow);
//        }
    }

    /**
     * 获取 数量最多的 才艺信息对象
     * @param data
     * @return
     */
    public static ArtShow getMaxCount(List<ArtShow> data){

        //1. 先倒叙排序 ，取第一个
        data.sort((a,b)->b.getCount()-a.getCount());
        return data.get(0);

    }


    /**
     * 获取已经表演过的学生的名字
     * @param data
     * @return
     */
    public static List<String> getShowedNames(List<ArtShow> data){

        ArrayList<String> result = new ArrayList<>();

        for (ArtShow artShow : data) {
            if (artShow.getStatus().equals(1)) {
                result.add(artShow.getStuName());
            }
        }

        return result;

    }



    public static void sortArtListByCount(List<ArtShow> data){
        data.sort( (a,b)->b.getCount()-a.getCount() );
    }

    /**
     * 获取才艺表演学生的名单List
     * @param data
     * @return
     */
    public static List<String> getStuNames(List<ArtShow> data){

        ArrayList<String> result = new ArrayList<>();

        // 从 data中获取学生的名字，放入result中
        // 如何遍历集合呢
        Iterator<ArtShow> iterator = data.iterator();
        while (iterator.hasNext()) {
            ArtShow artShow = iterator.next();
            String stuName = artShow.getStuName();
            result.add(stuName);
        }

        return result;

    }



    public static List<ArtShow> createList() throws FileNotFoundException {

        ArrayList<ArtShow> artShows = new ArrayList<>();

        // 填充 数据
//        ArtShow artShow = new ArtShow();
//        artShow.setStuName("丛伟龙");
//        artShow.setShowName("唱歌（影子）");
//        artShow.setStatus(1);
//        artShow.setCount(0);
        // 读取文本文件中的内容

        // Scanner对象
        // 文件的路径
        String filepath = "D:\\artshow.txt";
        File file = new File(filepath);
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            String line = scanner.nextLine();  // 读取一行数据
            // 解析字符串line，转换为 ArtShow对象
            ArtShow artShow = createArtShow(line);
            artShows.add(artShow);
        }

        scanner.close(); // 关闭扫描器对象
        return artShows;

    }

    private static ArtShow createArtShow(String line) {

        String s = "\t";
        String[] split = line.split(s);
        return new ArtShow(split[0], split[1], Integer.valueOf(split[2]), Integer.valueOf(split[3]));

    }

}
